<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-row>
        <el-form-item label="订单项id" prop="orderItemId">
          <el-input v-model="queryParams.orderItemId" placeholder="请输入订单项id" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="退货数量" prop="refundCount">
        <el-input
          v-model="queryParams.refundCount"
          placeholder="请输入退货数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
        <!-- <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
        <el-form-item label="退款说明" prop="refundInstructions">
          <el-input v-model="queryParams.refundInstructions" placeholder="请输入退款说明" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="是否同意" prop="isEnable">
          <template slot="label">
            <span>
              是否同意
              <el-tooltip class="item" effect="dark" content="不同意/同意" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="queryParams.isEnable" placeholder="请输入是否同意（不同意/同意）" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="queryParams.isDel" placeholder="请输入是否删除（0-正常，1-停用）" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="扩展字段1" prop="ex1">
        <el-input
          v-model="queryParams.ex1"
          placeholder="请输入扩展字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段2" prop="ex2">
        <el-input
          v-model="queryParams.ex2"
          placeholder="请输入扩展字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段3" prop="ex3">
        <el-input
          v-model="queryParams.ex3"
          placeholder="请输入扩展字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段4" prop="ex4">
        <el-input
          v-model="queryParams.ex4"
          placeholder="请输入扩展字段4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段5" prop="ex5">
        <el-input
          v-model="queryParams.ex5"
          placeholder="请输入扩展字段5"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8" style="display: flex;">
      <el-col style="justify-content: left;" :span="20">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:refund:add']">新增</el-button>
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:refund:edit']">修改</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:refund:remove']">删除</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:refund:export']">导出</el-button>
      </el-col>
      <el-col style="justify-content: right;" class="text-right" v-show="showSearch" :span="2.5">
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退货id" align="center" prop="refundId" />
      <el-table-column label="订单项id" align="center" prop="orderItemId" />
      <el-table-column label="退货数量" align="center" prop="refundCount" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="退款积分" align="center" prop="refundPoints" />
      <el-table-column label="退款说明" align="center" prop="refundInstructions" />
      <el-table-column label="退款说明图片" align="center" prop="refundImg">
        <template slot-scope="scope">
          <p v-if="scope.row.refundImg == null">无图片</p>
          <image-preview v-if="scope.row.refundImg != null" :src="scope.row.refundImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="是否同意" align="center" prop="isEnable" />
      <el-table-column label="删除标识" align="center" prop="isDel" />
      <!-- <el-table-column label="扩展字段1" align="center" prop="ex1" />
      <el-table-column label="扩展字段2" align="center" prop="ex2" />
      <el-table-column label="扩展字段3" align="center" prop="ex3" />
      <el-table-column label="扩展字段4" align="center" prop="ex4" />
      <el-table-column label="扩展字段5" align="center" prop="ex5" /> -->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['monitor:job:query']">详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改refund对话框 -->
    <el-dialog :title="title" :visible.sync="openEdit" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="订单项id" prop="orderItemId">
          <el-input v-model="form.orderItemId" placeholder="请输入订单项id" />
        </el-form-item>
        <el-form-item label="退货数量" prop="refundCount">
          <el-input v-model="form.refundCount" placeholder="请输入退货数量" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退款积分" prop="refundPoints">
          <el-input v-model="form.refundPoints" placeholder="请输入退款积分" />
        </el-form-item>
        <el-form-item label="退款说明" prop="refundInstructions">
          <el-input v-model="form.refundInstructions" placeholder="请输入退款说明" />
        </el-form-item>
        <el-form-item label="退款说明图片" prop="refundImg">
          <image-activityImg-upload v-model="form.refundImg" />
        </el-form-item>
        <el-form-item label="是否同意" prop="isEnable">
          <template slot="label">
            <span>
              是否同意
              <el-tooltip class="item" effect="dark" content="不同意/同意" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.isEnable" placeholder="请输入是否同意（不同意/同意）" />
        </el-form-item>
        <el-form-item label="不同意的理由" prop="noEnableReason">
          <el-input v-model="form.noEnableReason" placeholder="请输入不同意的理由"/>
        </el-form-item>
        <el-form-item label="是否删除" prop="isDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.isDel" placeholder="请输入是否删除（0-正常，1-停用）" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    
    <!-- 查看refund对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="订单项id" prop="orderItemId">
          <el-input v-model="form.orderItemId" placeholder="未填入订单项id" readonly/>
        </el-form-item>
        <el-form-item label="退货数量" prop="refundCount">
          <el-input v-model="form.refundCount" placeholder="未填入退货数量" readonly />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="未填入退款金额" readonly />
        </el-form-item>
        <el-form-item label="退款积分" prop="refundPoints">
          <el-input v-model="form.refundPoints" placeholder="未填入退款积分" readonly />
        </el-form-item>
        <el-form-item label="退款说明" prop="refundInstructions">
          <el-input v-model="form.refundInstructions" placeholder="未填入退款说明" readonly />
        </el-form-item>
        <el-form-item label="退款说明图片" prop="refundImg">
          <image-activityImg-upload v-model="form.refundImg" readonly />
        </el-form-item>
        <el-form-item label="是否同意" prop="isEnable">
          <template slot="label">
            <span>
              是否同意
              <el-tooltip class="item" effect="dark" content="不同意/同意" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.isEnable" placeholder="未填入是否同意（不同意/同意）" readonly />
        </el-form-item>
        <el-form-item label="不同意的理由" prop="noEnableReason">
          <el-input v-model="form.noEnableReason" placeholder="未填入不同意的理由" readonly />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.isDel" placeholder="未填入是否删除（0-正常，1-停用）" readonly />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listRefund, getRefund, delRefund, addRefund, updateRefund } from "@/api/system/refund";

export default {
  name: "Refund",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // refund表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openEdit: false,
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderItemId: null,
        refundCount: null,
        refundAmount: null,
        refundImg : null,
        refundInstructions: null,
        refundImg: null,
        isDel: null,
        isEnable: null,
        noEnableReason: null,
        ex1: null,
        ex2: null,
        ex3: null,
        ex4: null,
        ex5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询refund列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openEdit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        refundId: null,
        orderItemId: null,
        refundCount: null,
        refundAmount: null,
        refundPoints: null,
        refundInstructions: null,
        refundImg: null,
        isDel: null,
        isEnable: null,
        noEnableReason: null,
        createTime: null,
        updateTime: null,
        ex1: null,
        ex2: null,
        ex3: null,
        ex4: null,
        ex5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.refundId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openEdit = true;
      this.title = "添加refund";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const refundId = row.refundId || this.ids
      getRefund(refundId).then(response => {
        this.form = response.data;
        this.openEdit = true;
        this.title = "修改refund";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.refundId != null) {
            updateRefund(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openEdit = false;
              this.getList();
            });
          } else {
            addRefund(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openEdit = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openView = true;
      this.title = "查看用户信息管理（如需修改，请通过修改按钮进行修改）";
      this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const refundIds = row.refundId || this.ids;
      this.$modal.confirm('是否确认删除refund编号为"' + refundIds + '"的数据项？').then(function () {
        return delRefund(refundIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/refund/export', {
        ...this.queryParams
      }, `refund_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
